//
//  HCustomTableRow.m
//  sqliteToEnity
//
//  Created by hament on 13-8-18.
//  Copyright (c) 2013年 hament. All rights reserved.
//

#import "HCustomTableRow.h"


@interface HCustomTableRow ()
@property BOOL mouseInside;
@end




static NSGradient *gradientWithTargetColor(NSColor *targetColor) {
    NSArray *colors = [NSArray arrayWithObjects:[targetColor colorWithAlphaComponent:0], targetColor, targetColor, [targetColor colorWithAlphaComponent:0], nil];
    const CGFloat locations[4] = { 0.0, 0.35, 0.65, 1.0 };
    return [[NSGradient alloc] initWithColors:colors atLocations:locations colorSpace:[NSColorSpace sRGBColorSpace]];
}


@implementation HCustomTableRow



- (void)setMouseInside:(BOOL)value {
    if (_mouseInside != value) {
        _mouseInside = value;
        [self setNeedsDisplay:YES];
    }
}

- (BOOL)mouseInside {
    return _mouseInside;
}

- (void)ensureTrackingArea {
    if (_trackingArea == nil) {
        _trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:NSTrackingInVisibleRect | NSTrackingActiveAlways | NSTrackingMouseEnteredAndExited owner:self userInfo:nil];
    }
}

//callback
- (void)updateTrackingAreas {
    [super updateTrackingAreas];
    [self ensureTrackingArea];
    if (![[self trackingAreas] containsObject:_trackingArea]) {
        [self addTrackingArea:_trackingArea];
    }
}

- (void)mouseEntered:(NSEvent *)theEvent {
    self.mouseInside = YES;
}

- (void)mouseExited:(NSEvent *)theEvent {
    self.mouseInside = NO;
}


- (void)drawBackgroundInRect:(NSRect)dirtyRect {
    // Custom background drawing. We don't call super at all.
    [self.backgroundColor set];
    // Fill with the background color first
    NSRectFill(self.bounds);
    
    // Draw a white/alpha gradient
    if (self.mouseInside) {
//        NSGradient *gradient = gradientWithTargetColor([NSColor colorWithCalibratedRed:42.0/255.0 green:141.0/255.0 blue:32.0/255.0 alpha:1.0]);
        
     NSGradient *gradient = gradientWithTargetColor([NSColor colorWithCalibratedRed:237.0/255.0 green:175.0/255.0 blue:29.0/255.0 alpha:1.0]);
        
        [gradient drawInRect:self.bounds angle:0];
    }
}




- (void)drawSelectionInRect:(NSRect)dirtyRect {
    // Check the selectionHighlightStyle, in case it was set to None
    if (self.selectionHighlightStyle != NSTableViewSelectionHighlightStyleNone) {
        // We want a hard-crisp stroke, and stroking 1 pixel will border half on one side and half on another, so we offset by the 0.5 to handle this
        NSRect selectionRect = NSInsetRect(self.bounds, 4.0, 3);
        //[[NSColor colorWithCalibratedRed:24.0/255.0 green:111.0/255.0 blue:27.0/255.0 alpha:1.0] setStroke];
       // [[NSColor colorWithCalibratedRed:66.0/255.0 green:175.0/255.0 blue:40.0/255.0 alpha:1.0] setFill];
        [[NSColor colorWithCalibratedRed:203.0/255.0 green:121.0/255.0 blue:20.0/255.0 alpha:1.0] setStroke];
        [[NSColor colorWithCalibratedRed:254.0/255.0 green:232.0/255.0 blue:69.0/255.0 alpha:1.0] setFill];
        NSBezierPath *selectionPath = [NSBezierPath bezierPathWithRoundedRect:selectionRect xRadius:5 yRadius:5];
        [selectionPath fill];
        [selectionPath stroke];
    }
}

- (NSRect)separatorRect {
    NSRect separatorRect = self.bounds;
    separatorRect.origin.y = NSMaxY(separatorRect) - 2;
    separatorRect.size.height = 2;
    return separatorRect;
}

// Only called if the table is set with a horizontal grid
- (void)drawSeparatorInRect:(NSRect)dirtyRect {
    // Use a common shared method of drawing the separator
    DrawSeparatorInRect([self separatorRect]);
}



- (NSBackgroundStyle)interiorBackgroundStyle {
    return NSBackgroundStyleLight;
}


- (void)setFrame:(NSRect)frameRect {
    [super setFrame:frameRect];
    // We need to invalidate more things when live-resizing since we fill with a gradient and stroke
    if ([self inLiveResize]) {
        // Redraw everything if we are using a gradient
        if (self.selected || self.mouseInside) {
            [self setNeedsDisplay:YES];
        } else {
            // Redraw our horizontal grid line, which is a gradient
            [self setNeedsDisplayInRect:[self separatorRect]];
        }
    }
}

@end

//绘制分割线
void DrawSeparatorInRect(NSRect rect) {
    // Cache the gradient for performance
    static NSGradient *gradient = nil;
    if (gradient == nil) {
        gradient = gradientWithTargetColor([NSColor colorWithSRGBRed:.40 green:.40 blue:.40 alpha:1]) ;
    }
    [gradient drawInRect:rect angle:0];
    
}
